package com.hsteach.service.meeting;


import com.hsteach.common.core.domain.model.AuthInfo;
import com.hsteach.common.exception.ServiceException;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.common.utils.uuid.UUID;
import com.hsteach.dao.domain.buildingApply.BuildingApply;
import com.hsteach.dao.domain.meeting.MeetingPerson;
import com.hsteach.dao.mapper.buildingApply.BuildingApplyMapper;
import com.hsteach.dao.mapper.meeting.MeetingPersonMapper;
import com.hsteach.enums.BooleanEnum;
import com.hsteach.web.controller.meeting.dto.MeetingPersonDto;
import com.hsteach.web.controller.meeting.dto.MeetingPersonResponseDto;
import org.apache.commons.collections4.CollectionUtils;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class MeetingPersonService {

    @Resource
    private MeetingPersonMapper meetingPersonMapper;

    @Autowired
    private BuildingApplyMapper buildingApplyMapper;

    @Autowired
    private RedissonClient redissonClient;

    private final static String SIGN_CODE_CACHE = "SING_CODE:";

    public MeetingPersonResponseDto queryPersonInfo(Long applyId) {

        //未签到参会人员
        List<MeetingPerson> noSignInPerson = meetingPersonMapper.queryPersonInfoByType(applyId,0);

        //已签到参会人员
        List<MeetingPerson> signInPerson = meetingPersonMapper.queryPersonInfoByType(applyId,1);

        return new MeetingPersonResponseDto(noSignInPerson.size()+signInPerson.size(),signInPerson,noSignInPerson);
    }

    public int add(MeetingPersonDto dto) {
        MeetingPerson meetingPerson = new MeetingPerson();
        meetingPerson.setPersonId(dto.getPersonId());
        meetingPerson.setApplyId(dto.getApplyId());
        meetingPerson.setPersonName(dto.getPersonName());
        meetingPerson.setPersonType(dto.getPersonType());
        meetingPerson.setSignInStatus(0);
        return meetingPersonMapper.insert(meetingPerson);
    }

    public int signIn(Long applyId, String signCode) {
        RBucket<Long> bucket = redissonClient.getBucket(SIGN_CODE_CACHE + signCode);
        if (bucket.isExists()) {
            AuthInfo authInfo = SecurityUtils.getAuthInfo();
            Long aLong = bucket.get();
            if (!applyId.equals(aLong)) {
                throw new ServiceException("签到码与当前会议不一致");
            }
            MeetingPerson query = new MeetingPerson();
            query.setApplyId(applyId);
            query.setDelStatus(BooleanEnum.TRUE.getCode());
            query.setOrgCode(authInfo.getOrgCode());
            query.setPersonId(authInfo.getPersonId());
            List<MeetingPerson> meetingPeople = meetingPersonMapper.selectList(query);
            if (CollectionUtils.isEmpty(meetingPeople)) {
                throw new ServiceException("您不是本会议参会人员");
            }
            int size = meetingPeople.stream().filter(e -> BooleanEnum.FALSE.getCode().equals(e.getSignInStatus())).collect(Collectors.toList()).size();
            if (size == 0) {
                throw new ServiceException("请勿重复签到");
            }
            return meetingPersonMapper.signIn(applyId,authInfo.getPersonId());
        } else {
            throw new ServiceException("签到码已过期");
        }
    }

    public String getTeaOrgTree() {
            return null;
    }

    public String genSignCode(Long applyId) {
        BuildingApply apply = buildingApplyMapper.selectBuildingApplyById(applyId);
        if (apply == null || BooleanEnum.FALSE.getCode().equals(apply.getDelStatus())) {
            throw new ServiceException("会议不存在");
        }
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 9);
        RBucket<Long> bucket = redissonClient.getBucket(SIGN_CODE_CACHE + code);
        bucket.set(applyId, 12, TimeUnit.SECONDS);
        return code;
    }
}
